﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using BLL.RF.JointingCheck;
using Model.RF.JointingCheck;
using CrystalDecisions.CrystalReports.Engine;

public partial class ProductAssemble_ViewAssemble_QCPackCheck_QCPackCheckStatisticbyCIDDT : System.Web.UI.Page
{
    private string var_customerID
    {
        get { return ViewState["var_customerID"].ToString(); }
        set { ViewState["var_customerID"] = value; }
    }
    private DateTime var_starttime
    {
        get { return DateTime.Parse(ViewState["dt1"].ToString()); }
        set { ViewState["dt1"] = value; }
    }
    private DateTime var_stoptime
    {
        get { return DateTime.Parse(ViewState["dt2"].ToString()); }
        set { ViewState["dt2"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCustoms();
            this.Page.Unload += new EventHandler(Page_Unload);
        }
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
    }

    protected void BindCustoms()
    {
        Customer cust = new Customer();
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var_customerID = this.DropDownList1.SelectedValue;
        var_starttime = DateTime.Parse(this.TextBox1.Value.Trim());
        var_stoptime = DateTime.Parse(this.TextBox2.Value.Trim());
        IList<JointingFailure_QInfo> list = GetQData();
        if (list.Count > 0)
            BindQCR(list);
        else
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('数据为空！');</script>");
    }

    private void BindQCR(IList<JointingFailure_QInfo> list)
    {
        this.CrystalReportSource1.ReportDocument.Load(Server.MapPath("../QCFixedCheckStatisticbyDT.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        txtTestTime.Text = var_starttime.ToString() + "-" + var_stoptime.ToString();

        this.CrystalReportSource1.ReportDocument.SetDataSource(list);
        this.CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        this.CrystalReportViewer1.DataBind();
        this.CrystalReportViewer1.Visible = true;
    }

    private IList<JointingFailure_QInfo> GetQData()
    {
        IList<JointingFailure_QInfo> oprs = new List<JointingFailure_QInfo>();
        JointingFailure_Q pt = new JointingFailure_Q();
        oprs = pt.GetJointingFailure_Q_PackCheckbyCIDDT(var_customerID, var_starttime, var_stoptime);
        return oprs;
    }
}
